package com.example.course.utils.common.result;

import com.alibaba.fastjson.JSON;
import com.example.course.utils.common.enums.Code;

import java.util.ArrayList;
import java.util.List;

public class Result<T> {

    private Integer code;
    private String message;
    private T data;

    public Result() {
        this.code = 200;
    }

    public Result(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public static Result errorResult(int code, String msg) {
        Result result = new Result();
        return result.error(code, msg);
    }

    public static Result okResult(int code, String msg) {
        Result result = new Result();
        return result.ok(code, null, msg);
    }

    public static Result okResult(Object data) {
        Result result = setCode(Code.SUCCESS, Code.SUCCESS.getSuccess());
        if(data!=null) {
            result.setData(data);
        }
        return result;
    }

    public static Result errorResult(Code enums){
        return setCode(enums,enums.getSuccess());
    }

    public static Result errorResult(Code enums, String message){
        return setCode(enums,message);
    }

    public static Result setCode(Code enums){
        return okResult(enums.getCode(),enums.getSuccess());
    }

    private static Result setCode(Code enums, String message){
        return okResult(enums.getCode(),message);
    }

    public Result<?> error(Integer code, String msg) {
        this.code = code;
        this.message = msg;
        return this;
    }

    public Result<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public Result<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
        return this;
    }

    public Result<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getmessage() {
        return message;
    }

    public void setmessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static void main(String[] args) {
        //查询分页信息
        PageResponseResult responseResult = new PageResponseResult(1,5,50);
        List list = new ArrayList();
        list.add("jjShop");
        list.add("user_jj_shop");
        responseResult.setData(list);
        System.out.println(JSON.toJSONString(responseResult));
    }

}
